<?php
namespace app\index\service;


use app\index\model\BidModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertCompanyModel;
use app\index\model\CertModel;
use app\index\model\CertPerruModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidListModel;
use app\index\model\CertUseListModel;
use app\index\model\EnginModel;

class CertService extends CommonService
{

    public function check_cert_person_name($name, $id=0){
        return !!CertPersonModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0])->where('id','neq', $id)->find();
    }

    public function check_person_idcard_mate($idcard){
        return !!preg_match('/^\d{17}[0-9xX]$/', $idcard);
    }

    public function check_person_idcard_exist($idcard, $id=0){
        return !!CertPersonModel::where(['idcard'=>$idcard, 'comid'=>$this->userinfo['comid'] ,'deleted_user'=>0])->where('id','neq', $id)->find();
    }


    public function get_sex_by_idcard($idcard){
        $num = substr($idcard,-2,1);
        if(($num%2) == 1){
            $data = '男';
        }else{
            $data = '女';
        }
        return $data;
    }


    function get_age_by_idcard($idcard){
        if(!$idcard){
            return '';
        }

        if(!$this->check_person_idcard_mate($idcard)){
            return '';
        }

        $birth_Date = strtotime(substr($idcard, 6, 8));
        $Year = date('Y', $birth_Date);
        $Month = date('m', $birth_Date);
        $Day = date('d', $birth_Date);

        $current_Y = date('Y');
        $current_M = date('m');
        $current_D = date('d');

        $age = $current_Y - $Year;
        if($Month > $current_M || $Month == $current_M && $Day > $current_D){
            $age--;
        }
        return $age;
    }

    /**
     * 获取年份的月份天数
     */
    public function col_days_in_month($year,$month)
    {
        return cal_days_in_month(CAL_GREGORIAN,$month,$year);
    }


    public function get_cert_person_statueuse(){
        return [
            1 => '空闲',
            2 => '在用',
        ];
    }
    public function get_cert_person_statueuse_name($statueuse=0){
        $arr = $this->get_cert_person_statueuse();
        $str = isset($arr[$statueuse]) ? $arr[$statueuse] : '';
        return $str;
    }

    public function get_cert_statueuse(){
        return [
            1 => '空闲',
            3 => '投标在用',
            5 => '报建在用',
            7 => '报建在用(锁证)',
        ];
    }
    public function get_cert_statueuse_name($statueuse=0){
        $arr = $this->get_cert_statueuse();
        $str = isset($arr[$statueuse]) ? $arr[$statueuse] : '';
        return $str;
    }


    function get_cert_person_social_month($date1,$date2){
        if(!$date1 || $date1 == '0000-00-00'){
            return 0;
        }
        if(!$date2  || $date2 == '0000-00-00'){
            $date2 = date("Y-m-d");
        }
        if($date1 > $date2 || !$date1){
            return 0;
        }
        $date1 = date("Y-m-d",strtotime($date1.'-01'));
        $date2 = date("Y-m-d",strtotime($date2.'-01'));
        if(strtotime($date1)>strtotime($date2)){
            $tmp=$date2;
            $date2=$date1;
            $date1=$tmp;
        }
        list($Y1,$m1,$d1)=explode('-',$date1);
        list($Y2,$m2,$d2)=explode('-',$date2);
        $y=$Y2-$Y1;
        $m=$m2-$m1;
        $d=$d2-$d1;
        if($d<0){
            $d+=(int)date('t',strtotime("-1 month $date2"));
            $m--;
        }
        if($m<0){
            $m+=12;
            $y--;
        }
        $mounth = $y*12+$m+1;
        return $mounth;
    }



    public function get_cert_state(){
        return [
            1 => '正常',
            2 => '转出',
            3 => '注销',
            4 => '遗失',
            5 => '办理中',
            6 => '停用'
        ];
    }
    public function get_cert_state_name($state=0){
        $arr = $this->get_cert_state();
        $str = isset($arr[$state]) ? $arr[$state] : '';
        return $str;
    }

    public function get_cert_form(){
        return [
            1 => '纸质原件',
            2 => '电子件'
        ];
    }
    public function get_cert_form_name($form=0){
        $arr = $this->get_cert_form();
        $str = isset($arr[$form]) ? $arr[$form] : '';
        return $str;
    }

    public function get_cert_goes(){
        return [
            1 => '在册',
            2 => '借出'
        ];
    }
    public function get_cert_goes_name($goes=0){
        $arr = $this->get_cert_goes();
        $str = isset($arr[$goes]) ? $arr[$goes] : '';
        return $str;
    }




    public function get_cert_company_goes(){
        return [
            1 => '在册',
            2 => '借出'
        ];
    }
    public function get_cert_company_goes_name($goes=0){
        $arr = $this->get_cert_company_goes();
        $str = isset($arr[$goes]) ? $arr[$goes] : '';
        return $str;
    }

    public function get_cert_company_state(){
        return [
            1 => '正常',
            2 => '转出',
            3 => '注销',
            4 => '遗失',
            5 => '办理中',
            6 => '停用'
        ];
    }
    public function get_cert_company_state_name($state=0){
        $arr = $this->get_cert_company_state();
        $str = isset($arr[$state]) ? $arr[$state] : '';
        return $str;
    }


    public function get_cert_company_form(){
        return [
            1 => '纸质原件',
            2 => '电子件'
        ];
    }
    public function get_cert_company_form_name($form=0){
        $arr = $this->get_cert_company_form();
        $str = isset($arr[$form]) ? $arr[$form] : '';
        return $str;
    }


    public function get_cert_zhao_jindu(){
        return ['紧急', '一般'];
    }






    public function get_cert_borrow_lingtype(){
        return [
            1 => '快递',
            2 => '自取',
        ];
    }
    public function get_cert_borrow_lingtype_name($lingtype=0){
        $arr = $this->get_cert_borrow_lingtype();
        $str = isset($arr[$lingtype]) ? $arr[$lingtype] : '';
        return $str;
    }


    public function get_cert_borrow_state(){
        return [
            0 => '待取证',
            1 => '待归还',
            2 => '已归还',
        ];
    }
    public function get_cert_borrow_state_name($state=0){
        $arr = $this->get_cert_borrow_state();
        $str = isset($arr[$state]) ? $arr[$state] : '';
        return $str;
    }












    /**
     * 获取当前年的天数
     * @param $year
     * @return int
     */
    public function cal_days_in_year($year){
        $days=0;
        for($month=1;$month<=12;$month++){
            $days = $days + cal_days_in_month(CAL_GREGORIAN,$month,$year);
        }
        return $days;
    }

    /**
     * 设置人员证书、企业证书去向
     * $type 1人员 2企业
     */
    public function set_cert_goes($certid, $type)
    {
        $exists = CertBorrowListModel::alias('list')->field('list.*')
            ->leftJoin('cert_borrow bor','bor.id = list.mid')
            ->where(['bor.deleted_user'=>0,'bor.borrow_type'=>1,'bor.is_borrow'=>2,'bor.status'=>1,'bor.isturn'=>1,
                'list.certid'=>$certid,'list.is_return'=>'1','list.type'=>$type])
            ->find();
        if(!$exists){
            $goes = 1;
        }else{
            $goes = 2;
        }
        if($type == 1){
            CertModel::where('id',$certid)->update(['goes'=>$goes]);
        }else{
            CertCompanyModel::where('id',$certid)->update(['goes'=>$goes]);
        }
    }












    // 投标在用项目
    public function get_cert_use_bid($certid)
    {
        $enginArr = [];
        $cert_data = CertModel::where('id',$certid)->find();
        if($cert_data['statueuse'] == 3){
            $list = CertUseBidListModel::alias('list')
                ->field('use.bidid')
                ->leftJoin('cert_use_bid use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->select()->toArray();
            foreach ($list as $item){
                $bidinfo = BidModel::where(['id'=>$item['bidid']])->find();
                $enginArr[] = ['id'=>$bidinfo['id'],'name'=>$bidinfo['name']];
            }
        }
        return $enginArr;
    }

    // 报建在用项目
    public function get_cert_use_engin($certid)
    {
        $enginArr = [];
        $cert_data = CertModel::where('id',$certid)->find();
        if($cert_data['statueuse'] == 5 || $cert_data['statueuse'] == 7){
            $list = CertUseListModel::alias('list')
                ->field('use.enginid')
                ->leftJoin('cert_use use', 'use.id = list.useid')
                ->where(['use.comid' => $this->userinfo['comid'], 'use.deleted_user' => 0])
                ->where(['use.isturn' => 1, 'use.status' => 1, 'use.isreal' => 1, 'list.opt' => 1])
                ->select()->toArray();
            foreach ($list as $item){
                $engininfo = EnginModel::where(['id'=>$item['enginid']])->find();
                $enginArr[] = ['id'=>$engininfo['id'],'name'=>$engininfo['name']];
            }
        }
        return $enginArr;
    }








}
























